<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Sprite</title>
  <style>
    body {
      margin: 0;
      overflow: hidden;
      /* 隐藏body窗口区域滚动条 */
    }
  </style>
  <!--引入three.js三维引擎-->
  <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.js"></script>
  <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/examples/js/controls/OrbitControls.js"></script>
  <!-- <script src="./three.js"></script> -->
  <!-- <script src="http://www.yanhuangxueyuan.com/threejs/build/three.js"></script> -->
</head>

<body>
  <script src="static/js/main.js"></script>
  <script>
    /**
     * 创建场景对象Scene
     */
    let scene = new THREE.Scene();
    
    mesh = genMesh(()=> {
        let spriteMaterial = new THREE.SpriteMaterial({
            // 设置精灵矩形区域颜色
            color:0xffffff,
            // 旋转精灵对象45度，弧度值
            rotation:Math.PI/4,
            // 设置精灵纹理贴图
            map: new THREE.TextureLoader().load('static/img/light.png'),
        })
        let sprite = new THREE.Sprite(spriteMaterial)
        sprite.scale.set(100, 100, 1)
        sprite.position.y = 100
        return sprite
    })
    console.log(mesh)
    scene.add(mesh); //网格模型添加到场景中
    
    let rains = genMesh(() => {
        let textureRain = new THREE.TextureLoader().load("static/img/rain.png")
        let group = new THREE.Group()
        for (let i = 0; i < 400; ++i) {
            let spriteMaterial = new THREE.SpriteMaterial({
                // 设置精灵纹理贴图
                map: textureRain,
            })
            let sprite = new THREE.Sprite(spriteMaterial)
            sprite.scale.set(5, 8, 1)
            let k1 = Math.random() - 0.5;
            let k2 = Math.random() - 0.5;
            let k3 = Math.random() - 0.5;
            // 设置精灵模型位置，在整个空间上上随机分布
            let side = 500
            sprite.position.set(side * k1, side*k3, side * k2)
            group.add(sprite)
        }
        return group
    })
    scene.add(rains)
    
    
    mesh = genMesh({
        geometry: new THREE.PlaneGeometry(1000, 1500),
        material: new THREE.MeshLambertMaterial({
            color: 0x44dd44,
            side: THREE.DoubleSide,
        }),
    })
    mesh.rotateX(Math.PI / 2)
    scene.add(mesh)
    
    let axisHelper = new THREE.AxesHelper(250);
    scene.add(axisHelper);
    /**
     * 光源设置
     */
    //点光源
    let point = new THREE.PointLight(0xffffff);
    point.position.set(400, 200, 300); //点光源位置
    scene.add(point); //点光源添加到场景中
    //环境光
    let ambient = new THREE.AmbientLight(0x888888);
    scene.add(ambient);
    // console.log(scene)
    // console.log(scene.children)
    /**
     * 相机设置
     */
    let width = window.innerWidth; //窗口宽度
    let height = window.innerHeight; //窗口高度
    let k = width / height; //窗口宽高比
    let s = 200; //三维场景显示范围控制系数，系数越大，显示的范围越大
    //创建相机对象
    let camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
    // camera = new THREE.PerspectiveCamera(60, k, 1, 1000)
    camera.position.set(0, 200, 600); //设置相机位置
    camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
    /**
     * 创建渲染器对象
     */
    let renderer = new THREE.WebGLRenderer();
    renderer.setSize(width, height);//设置渲染区域尺寸
    renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色
    document.body.appendChild(renderer.domElement); //body元素中插入canvas对象
    
    window.onresize = function() {
        let width = window.innerWidth; //窗口宽度
        let height = window.innerHeight; //窗口高度
        renderer.setSize(width, height);//设置渲染区域尺寸
        
        k = width / height; //窗口宽高比
        //修改相机对象属性
        camera.left = -s * k
        camera.right = s * k
        camera.top = s
        camera.bottom = -s
        camera.aspect = k
        camera.updateProjectionMatrix ()
    }
    
    Game = {
		tickLength: 16, 				// 20Hz
		lastTick: performance.now(),	// 最后更新时间
		stopKey: 0,
		canvas: null,
		context: null,
	}
    function render(tFrame) {
        if (tFrame === undefined) {
            tFrame = performance.now()
            Game.lastTick = performance.now()
        }
        let nextTick = Game.lastTick + Game.tickLength
        let tickNum = 0
        if (tFrame > nextTick) {
            tickNum = Math.floor((tFrame - Game.lastTick) / Game.tickLength)
        }
        Game.lastTick = tFrame
        /*
        for (let num = 0; num < tickNum; ++num) {
            for (let i = 0, l = meshes.length; i < l; ++i) {
                meshes[i].position.set((i + 0.5 - l / 2) * 150, 0, 0)
                meshes[i].rotateY(3.14 / 180)
            }
        }
        */
        rains.children.forEach(rain => {
            let top = 1000
            let offy = getV(top - rain.position.y)
            rain.position.y -= offy
            if (rain.position.y < 0) {
                rain.position.y = top * (Math.random() * 0.5 + 0.5)
            }
        })
        //执行渲染操作   指定场景、相机作为参数
        renderer.render(scene, camera);
        Game.stopKey = requestAnimationFrame(render)
    }
    function getV(distance) {
        let g = 0.5
        let t = Math.sqrt(distance * 2 / 9)
        return g * t
    }
    render()
    let controls = new THREE.OrbitControls(camera,renderer.domElement);//创建控件对象
    // controls.addEventListener('change', render);
  </script>
</body>
</html>
